package chamans.life{
	// google speech api
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;

	
	public class Speech extends EventDispatcher
	{
		private var str:String = "";
		private var wordIndex:int = 0;
		private var words:Vector.<String>;
		private var queries:Vector.<String>;
		private var sp:Sprite = new Sprite();
		private var loadIndex:int = 0;
		private var sounds:Vector.<Sound>;
		private var channel:SoundChannel = new SoundChannel();
		private var s:Sound;
		private var transform:SoundTransform = new SoundTransform(0.1, 1.1);

		private var speakIndex:int = 0;
		
		private var Blend:int = 400;
		
		public function Speech(_str:String="") 
		{
			if (_str != ""){
				read(_str);
			}
		}
		public function read(_str:String):void
		{
			addEventListener("FirstClipLoaded", beginPlaying);
			load(_str);
		}
		private function beginPlaying(event:Event):void
		{
			play();
		}
		public function play():void
		{
			stop();
			wordIndex = 0;
			loadIndex = 0;
			speakIndex = 0;
			
			Speak();
		}
		public function stop():void
		{
			channel.stop();
			sp.removeEventListener(Event.ENTER_FRAME, WaitForEnd);
			s.removeEventListener(Event.COMPLETE, doLoadComplete);
		}
		public function load(_str:String):void
		{
			str = _str;
			words = Vector.<String>(str.split(' '));
			queries = new Vector.<String>();
			sounds = new Vector.<Sound>();
			
			wordIndex = 0;
			loadIndex = 0;
			speakIndex = 0;
			
			query(wordIndex);
			loadSpeech(loadIndex);
		}
		private function query(startIndex:int):void
		{
			var val:String = "";
			var limit:Boolean = false;
			for (var i:int = startIndex; i < words.length; ++i){
				if (val.length + words[i].length < 90 && !limit){
					if (i != startIndex) val += " ";
					val += words[i];
					wordIndex = i+1;
				}
				else limit = true;
			}
			queries.push(val);
			if (wordIndex < words.length-1) query(wordIndex);
		}
		private function loadSpeech(index:int):void
		{
			var url:String = 'http://translate.google.com/translate_tts?tl=en&q=' + queries[index];
			s = new Sound(new URLRequest(url));
			s.addEventListener(Event.COMPLETE, doLoadComplete);
		}
		private function doLoadComplete(event:Event):void
		{
			sounds.push(s);
			if (loadIndex == 0) dispatchEvent(new Event("FirstClipLoaded"));
			
			loadIndex++;
			if (loadIndex < queries.length) loadSpeech(loadIndex);
			else dispatchEvent(new Event(Event.COMPLETE));
		}
		
		
		private function Speak():void
		{
			channel = new SoundChannel();
			//channel.soundTransform.volume=.1;
			

			channel = sounds[speakIndex].play(0.5);
			channel.soundTransform = transform;
			
			sp.addEventListener(Event.ENTER_FRAME, WaitForEnd);
			
		}
		private function WaitForEnd(event:Event):void
		{
			if (sounds.length > 0){
				if (channel.position > sounds[speakIndex].length - Blend){
					sp.removeEventListener(Event.ENTER_FRAME, WaitForEnd);
					speakIndex++;
					if (speakIndex < sounds.length) Speak();
				}
			}
		}
	}
}
